﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;

namespace mfe_proj.Forms
{
    public partial class BookForm : BaseDetailForm
    {
        string _ItemGuidID;
        public ItemBook book = null;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public BookForm()
        {
            InitializeComponent();
        }

        public Panel DockablePanel
        {
            get { return panel1; }
            set { panel1 = value; }
        }

        public void LoadData()
        {
            LoadEdition();
            LoadPublisherList();

            //edit
            if (ItemGuidID != null && ItemGuidID != string.Empty)
            {
                //general
                book = new ItemBook(new Guid(ItemGuidID));
                ymdPublishDate.Year = (int?)book.PublishYear;
                ymdPublishDate.Month = (int?)book.PublishMonth;
                ymdPublishDate.Day = (int?)book.PublishDay;
                txtTitle1.Text = book.Title1 != null ? book.Title1 : string.Empty;
                txtTitle2.Text = book.Title2 != null ? book.Title2 : string.Empty;
                txtTranslator1.Text = book.Translator1 != null ? book.Translator1 : string.Empty;
                txtTranslator2.Text = book.Translator2 != null ? book.Translator2 : string.Empty;
                txtPrice.Text = book.Price != null ? book.Price : string.Empty;
                txtPageCount.Text = book.PageCount.ToString();
                txtTOC.Text = book.TableContent != null ? book.TableContent : string.Empty;
                txtPreface.Text = book.Preface != null ? book.Preface : string.Empty;
                txtISBN10.Text = book.ISBN10 != null ? book.ISBN10 : string.Empty;
                txtISBN13.Text = book.ISBN13 != null ? book.ISBN13 : string.Empty;
                txtNote.Text = book.Note != null ? book.Note : string.Empty;
                cbEdition.SelectedValue = book.Edition - 1;
                if (book.PublisherGuidID != null && book.PublisherGuidID != string.Empty)
                    cbPublisher.SelectedValue = book.PublisherGuidID;
                if (book.ItemPersonGuidID != null && book.ItemPersonGuidID != string.Empty)
                    cbAuthor.SelectedValue = book.ItemPersonGuidID;
            }
        }


        public override string ValidateData()
        {
            StringBuilder sb = new StringBuilder();
            if (cbPublisher.SelectedIndex == -1)
                sb.AppendLine("Publisher needs to be selected.");

            return sb.ToString();
        }

        public override bool SaveData(string itemGuidID)
        {
            bool retval = false;
            try
            {
                if (book == null || book.BookID == 0) book = new ItemBook();
                book.ItemGuidID = itemGuidID;
                if (ymdPublishDate.Year.HasValue)
                    book.PublishYear = ymdPublishDate.Year.Value;
                if (ymdPublishDate.Month.HasValue)
                    book.PublishMonth = ymdPublishDate.Month.Value;
                if (ymdPublishDate.Day.HasValue)
                    book.PublishDay = ymdPublishDate.Day.Value;
                book.Title1 = txtTitle1.Text;
                book.Title2 = txtTitle2.Text;
                book.Translator1 = txtTranslator1.Text;
                book.Translator2 = txtTranslator2.Text;
                if (txtPrice.Text.Trim().Length > 0)
                    book.Price = txtPrice.Text.Trim();
                if (txtPageCount.Text.Trim().Length > 0)
                    book.PageCount = int.Parse(txtPageCount.Text.Trim());
                book.TableContent = txtTOC.Text;
                book.Preface = txtPreface.Text;
                book.ISBN10 = txtISBN10.Text.Trim();
                book.ISBN13 = txtISBN13.Text.Trim();
                book.Edition = cbEdition.SelectedIndex + 1;
                if (cbBinding.SelectedIndex != -1)
                    book.Edition = (int)cbBinding.SelectedValue;
                if (cbPublisher.SelectedIndex != -1)
                    book.PublisherGuidID = cbPublisher.SelectedValue.ToString();
                if (cbAuthor.SelectedIndex != -1)
                    book.ItemPersonGuidID = cbAuthor.SelectedValue.ToString();
                book.Note = txtNote.Text.Trim();
                book.ItemBookGuidID = Guid.NewGuid().ToString();
                //save
                retval = (book.BookID == 0) ? book.Save() : book.Update();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retval;
        }

        void LoadEdition()
        {
            for (int i = 1; i <= 20; i++)
                cbEdition.Items.Add(i.ToString());
        }

        void LoadPublisherList()
        {
            if (Program.PublisherCollection == null) Program.PublisherCollection = Publisher.PublisherCollection(null, null);

            cbPublisher.DataSource = Program.PublisherCollection;
            cbPublisher.DisplayMember = "Name";
            cbPublisher.ValueMember = "PublisherGuidID";
            cbPublisher.SelectedIndex = -1;
        }

        private void lbAddNewPublisher_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PublisherForm pubForm = new PublisherForm();
            pubForm.PublisherAdded += new PublisherFormEventHandler(pubForm_PublisherAdded);
            pubForm.ShowDialog();
        }

        void pubForm_PublisherAdded(object sender, EventArgs e)
        {
            if (Program.PublisherCollection != null)
            {
                Program.PublisherCollection.Clear();
                Program.PublisherCollection = null;
            }
            LoadPublisherList();
        }
    }
}
